<template>
  <div class="treatment-care">
    <!-- 诊疗护理内容 -->
    <el-card class="treatment-container">
      <!-- 患者信息栏 -->
      <div class="patient-info-bar">
        <!-- 左边1/5宽度 -->
        <div class="patient-basic">
          <div class="avatar">
            <div class="id-avatar">
              <img v-if="patient.avatar" :src="patient.avatar" alt="patient-avatar" class="avatar-image" />
              <img v-else :src="patient.gender === '男' ? '/src/assets/svg/男士.svg' : '/src/assets/svg/女士.svg'"
                alt="gender-icon" class="gender-icon" />
            </div>
          </div>
          <div class="info">
            <div class="left-column">
              <span class="bed-number">{{ patient.bedNumber }}</span>
              <span class="name">{{ patient.patientName }}</span>
            </div>
            <div class="right-column">
              <el-icon :color="getCriticalColor(patient.criticalLevel)" style="font-size: 24px">
                <StarFilled />
              </el-icon>
              <span class="gender-age">{{ patient.gender }} {{ patient.age }}岁</span>
            </div>
          </div>
        </div>

        <!-- 右边4/5宽度 -->
        <div class="patient-details">
          <div class="detail-item">
            <div class="detail-row">
              <span class="label">病案号:</span>
              <span>{{ patient.caseNumber }}</span>
            </div>
            <div class="detail-row">
              <span class="label">诊断:</span>
              <span>{{ patient.diagnosis }}</span>
            </div>
          </div>
          <div class="detail-item">
            <div class="detail-row">
              <span class="label">ID:</span>
              <span>{{ patient.patientId }}</span>
            </div>
            <div class="detail-row">
              <span class="label">病人费别:</span>
              <span>{{ patient.paymentType }}</span>
            </div>
          </div>
          <div class="detail-item">
            <div class="detail-row">
              <span class="label">入院日期:</span>
              <span>{{ patient.admissionDate }}</span>
            </div>
            <div class="detail-row">
              <span class="label">时间:</span>
              <span>{{ patient.admissionDays }}</span>
            </div>
          </div>
          <div class="detail-item">
            <div class="detail-row">
              <span class="label">入科日期:</span>
              <span>{{ patient.departmentAdmissionDate }}</span>
            </div>
            <div class="detail-row">
              <span class="label">过敏史:</span>
              <span>{{ patient.allergyHistory }}</span>
            </div>
          </div>
          <div class="detail-item">
            <div class="detail-row">
              <span class="label">主治医生:</span>
              <span>{{ patient.attendingDoctor }}</span>
            </div>
            <div class="detail-row">
              <span class="label">护士责任:</span>
              <span>{{ patient.responsibleNurse }}</span>
            </div>
          </div>
          <div class="detail-item">
            <div class="detail-row">
              <span class="label">危急值:</span>
              <span>{{ patient.criticalValues }}</span>
            </div>
          </div>
        </div>
      </div>

      <!-- 操作工具栏 -->
      <div class="action-toolbar">
        <el-button plain size="mini" :class="{ active: activeTab === 'overview' }" @click="switchTab('overview')">
          患者总览
        </el-button>
        <el-button plain size="mini" :class="{ active: activeTab === 'organ' }" @click="switchTab('organ')">
          器官功能概览
        </el-button>
        <el-button plain size="mini" :class="{ active: activeTab === 'score' }" @click="switchTab('score')">
          医生评分
        </el-button>
        <el-button plain size="mini" :class="{ active: activeTab === 'documents' }" @click="switchTab('documents')">
          病历文书
        </el-button>
        <el-button plain size="mini" :class="{ active: activeTab === 'report' }" @click="switchTab('report')">
          患者报告
        </el-button>
      </div>

      <div class="patient-main-content">
        <component :is="currentComponent" />
      </div>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, shallowRef } from 'vue'
import PatientOverview from './PatientOverview.vue'
import OrganFunction from './OrganFunction.vue'
import DoctorScore from './DoctorScore.vue'
import MedicalDocuments from './MedicalDocuments.vue'
import PatientReport from './PatientReport.vue'
import { StarFilled } from '@element-plus/icons-vue'
import { ElAvatar, ElIcon } from 'element-plus'

interface Patient {
  id: string
  bedNumber: string
  patientId: string
  patientName: string
  gender: string
  age: number
  caseNumber: string
  diagnosis: string
  criticalLevel: number
  avatar?: string
  paymentType: string
  admissionDate: string
  admissionDays: string
  departmentAdmissionDate: string
  allergyHistory: string
  attendingDoctor: string
  responsibleNurse: string
  criticalValues: string
}

const activeTab = ref('overview');
const currentComponent = shallowRef(PatientOverview);

const switchTab = (tabName: string) => {
  activeTab.value = tabName;
  switch (tabName) {
    case 'overview':
      currentComponent.value = PatientOverview;
      break;
    case 'organ':
      currentComponent.value = OrganFunction;
      break;
    case 'score':
      currentComponent.value = DoctorScore;
      break;
    case 'documents':
      currentComponent.value = MedicalDocuments;
      break;
    case 'report':
      currentComponent.value = PatientReport;
      break;
    default:
      currentComponent.value = PatientOverview;
  }
};

const patient = ref<Patient>({
  id: '1',
  bedNumber: 'ICU-001',
  patientId: 'MO2356789',
  patientName: '张三',
  gender: '男',
  age: 45,
  caseNumber: '5120132',
  diagnosis: '脾破裂肝多发脑损伤',
  criticalLevel: 2,
  avatar: '',
  paymentType: '自费',
  admissionDate: '2018-9-18 12:00',
  admissionDays: '1天',
  departmentAdmissionDate: '2018-9-19 13:00',
  allergyHistory: '异类胺',
  attendingDoctor: '张三',
  responsibleNurse: '李珊珊',
  criticalValues: 'K:5 Na:19 Cl-:'
})

const getCriticalColor = (level?: number) => {
  switch (level) {
    case 3: return '#FF0000' // 红色表示最高危急
    case 2: return '#FF9900' // 橙色表示中等危急
    case 1: return '#F7BA2A' // 黄色表示低危急
    default: return '#99A9BF' // 灰色表示无危急
  }
}
</script>

<style scoped>
.treatment-care {
  padding: 10px;
  height: 100vh;
  box-sizing: border-box;
}

.patient-info-bar {
  display: flex;
  gap: 10px;
  margin-bottom: 15px;
}

.patient-basic {
  width: 15%;
  display: flex;
  align-items: center;
  gap: 15px;
  background: #fff;
  padding: 12px;
  border-radius: 4px;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.1);
  border: 1px solid #ebeef5;
}

.id-avatar {
  max-width: 62px;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: transparent;
  /* border: 1px solid #e0e0e0; */
  /* border-radius: 4px; */
  overflow: hidden;
}

.avatar-image {
  width: 100%;
  height: 100%;
  object-fit: fill;
}

.gender-icon {
  width: 100%;
  height: 100%;
  object-fit: fill;
}

.info {
  display: flex;
  width: 100%;
  justify-content: space-between;
}

.left-column {
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.right-column {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 8px;
}

.bed-number {
  font-weight: bolder;
  font-size: 18px;
}

.name {
  font-weight: bold;
  font-size: 16px;
}

.gender-age {
  color: #666;
  font-weight: normal;
  font-size: 14px;
  margin-left: 15px;
}

.patient-details {
  width: 85%;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 4px;
  background: #fff;
  padding: 5px;
  border-radius: 4px;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.1);
  border: 1px solid #ebeef5;
}

.detail-item {
  display: flex;
  flex-direction: column;
  gap: 1px;
  background-color: #f9f9f9;
  padding: 4px;
  border-radius: 4px;
}

.detail-row {
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  align-items: center;
  gap: 4px;
}

.label {
  font-weight: bold;
  color: #666;
  font-size: 14px;
  text-align: right;
  min-width: 60px;
}

.detail-row span:not(.label) {
  font-size: 14px;
  color: #333;
  text-align: left;
  flex: 1;
  max-width: calc(100% - 65px);
  word-wrap: break-word;
  white-space: normal;
  overflow-wrap: break-word;
}

.treatment-container {
  margin-top: 0px;
  display: flex;
  flex-direction: column;
  height: 100vh;
}

.patient-main-content {
  flex: 1;
  border: 1px solid #ebeef5;
  border-radius: 0;
  margin-top: 10px;
  min-height: 780px;
  overflow: auto;
  height: 0;
  /* 关键设置，使flex:1生效 */
}

.action-toolbar {
  margin-top: 0px;
  display: flex;
  gap: 1px;
  padding: 0;
}

.action-toolbar .el-button {
  flex: 1;
  max-width: 7em;
  padding: 0 10px;
  border-radius: 0px;
}

.action-toolbar .el-button.active {
  background-color: var(--bg-active);
  border-color: #c6e2ff;
  color: var(--text-active)
}

::v-deep .el-button+.el-button {
  margin-left: -1px;

}
</style>
